/*
 * Copyright (C) 2011 Wglxy.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netik.main;
import com.netik.adapters.*;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;




import android.os.Bundle;
import android.os.Environment;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * This is the activity for feature 2 in the dashboard application.
 * It displays some text and provides a way to get back to the home activity.
 *
 */

public class Favorites extends DashboardActivity 
{

private String[] enqid;
private String[] enqname;
private ListView dataListView;
private ArrayList<Data> datalist;
private DataViewAdapter dataAdapter;
private NodeList listOfDocs;
private String[] temp;

/**
 * onCreate
 *
 * Called when the activity is first created. 
 * This is where you should do all of your normal static set up: create views, bind data to lists, etc. 
 * This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.
 * 
 * Always followed by onStart().
 *
 * @param savedInstanceState Bundle
 */

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView (R.layout.enqlist);
    setTitleFromActivityLabel (R.id.title_text);
    
    File sdcard = Environment.getExternalStorageDirectory();

	
	File file = new File(sdcard,"fav1.txt");
	
	try {

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse (file);

        // normalize text representation
        doc.getDocumentElement ().normalize ();
        System.out.println ("Root element of the doc is " + 
             doc.getDocumentElement().getNodeName());

        
        StringBuilder text = new StringBuilder();

		try {
		    BufferedReader br = new BufferedReader(new FileReader(file));
		    String line;

		    while ((line = br.readLine()) != null) {
		        text.append(line);
		        text.append('\n');
		    }
		}
		catch (IOException e) {
		    //You'll need to add proper error handling here
		}

        System.out.println("here=="+text);
        
        listOfDocs = doc.getElementsByTagName("doc");
        int totalDocs = listOfDocs.getLength();
        System.out.println("Total no of results : " + totalDocs);
        
        enqid = new String[totalDocs+1];
        enqname = new String[totalDocs+1];
        
        for(int s=0; s<listOfDocs.getLength() ; s++){


            Node firstPersonNode = listOfDocs.item(s);
            if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){


                Element firstPersonElement = (Element)firstPersonNode;

                //-------
                NodeList firstNameList = firstPersonElement.getElementsByTagName("Enq_id");
                Element firstNameElement = (Element)firstNameList.item(0);

                NodeList textFNList = firstNameElement.getChildNodes();
                /*System.out.println("QUANTITY : " + 
                       ((Node)textFNList.item(0)).getNodeValue().trim());*/
                
                enqid[s] = ((Node)textFNList.item(0)).getNodeValue().trim().toString();
               
                //-------
                NodeList lastNameList = firstPersonElement.getElementsByTagName("Enq_name");
                
                Element lastNameElement = (Element)lastNameList.item(0);
                
                NodeList textLNList = lastNameElement.getChildNodes();
                System.out.println("ISS_NME : " + 
                       ((Node)textLNList.item(0)).getNodeValue().trim());
                enqname[s] = ((Node)textLNList.item(0)).getNodeValue().trim().toString();

                //----
               

            }//end of if clause


        }//end of for loop with s var


    }catch (SAXParseException err) {
    System.out.println ("** Parsing error" + ", line " 
         + err.getLineNumber () + ", uri " + err.getSystemId ());
    System.out.println(" " + err.getMessage ());

    }catch (SAXException e) {
    Exception x = e.getException ();
    ((x == null) ? e : x).printStackTrace ();

    }catch (Throwable t) {
    t.printStackTrace ();
    }
	temp= new String[1000];
	dataListView = (ListView) findViewById(R.id.datalist);
    
    //datalist = new ArrayList<Data>();
    //dataAdapter = new DataViewAdapter(getApplicationContext(), datalist);

    for(int i=0; i<listOfDocs.getLength(); i++)
    {
    	/*String name= enqid[i];
    	String id1 = enqname[i]; 
    	//String value = resPrice[i];
    	Data team1 = new Data(id1,name);
    	datalist.add(team1);*/
    	temp[i] = enqid[i]+"\t"+enqname[i];
    }
    
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
			android.R.layout.simple_list_item_1, temp);
	dataListView.setAdapter(adapter);
	adapter.notifyDataSetChanged();
    
}
    
} // end class
